package com.ag.zrt.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

import com.ag.bean.TbCurrentEvent;
import com.ag.bean.TbEvent;
import com.ag.bean.TbLogPlanStep;
import com.ag.bean.TbLogPsContent;
import com.ag.bean.TbPlan;
import com.ag.bean.TbPlanStep;
import com.ag.bean.TbPsContent;
import com.ag.common.AgException;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.processing.ProcessingPlans;
import com.ag.wzp.dao.WzpTbEventDAO;
import com.ag.zrt.dao.TbCurrentEventDAO;
import com.ag.zrt.dao.TbLogPlanStepDAO;
import com.ag.zrt.dao.TbPlanStepDAO;
import com.ag.zrt.dao.TbPsContentDAO;
import com.ag.zrt.service.IPlanStepService;

@Service("PlanStepService")
public class PlanStepServiceImpl implements IPlanStepService {
	@Resource(name = "ZRTTbPlanStepDAO")
	private TbPlanStepDAO planStepDAO;// 处理步骤的DAO
	@Resource(name = "ZRTTbPsContentDAO")
	private TbPsContentDAO psContentDAO;// 处理步骤内容的DAO
	@Resource(name = "ZRTTbCurrentEventDAO")
	private TbCurrentEventDAO currentEventDAO;// 处理当前事件的当前步骤序号
	@Resource(name = "WzpTbEventDAO")
	private WzpTbEventDAO eventDAO;
	@Resource(name = "ZRTTbLogPlanStepDAO")
	private TbLogPlanStepDAO logPlanStepDAO;
	@Resource(name = "iDServiceImpl")
	private IIDService IdService;

	public void setIdService(IIDService idService) {
		IdService = idService;
	}

	public void setLogPlanStepDAO(TbLogPlanStepDAO logPlanStepDAO) {
		this.logPlanStepDAO = logPlanStepDAO;
	}

	public void setEventDAO(WzpTbEventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	public void setCurrentEventDAO(TbCurrentEventDAO currentEventDAO) {
		this.currentEventDAO = currentEventDAO;
	}

	public void setPsContentDAO(TbPsContentDAO psContentDAO) {
		this.psContentDAO = psContentDAO;
	}

	public void setPlanStepDAO(TbPlanStepDAO planStepDAO) {
		this.planStepDAO = planStepDAO;
	}

	/*
	 * 通过预案ID找到相应预案的步骤
	 */
	@Override
	public List<TbLogPlanStep> findByProperty(String planId, Object value,
			String eventId) {
		// TODO Auto-generated method stub
		System.out
				.println(value
						+ "*/****************************planid****************************");
		return createLogPlanStepList(planStepDAO.findByProperty(planId, value),
				eventId);
	}

	/*
	 * 复制步骤内容
	 */
	@Override
	public void createLogPlanStepContent(String planStepId, String logPlanStepId) {
		// TODO Auto-generated method stub

		/*
		 * 根据预案步骤ID找出对应的内容
		 */
		List<TbPsContent> contentList = psContentDAO.findByProperty(
				"tbPlanStep.psId", planStepId);
		Session session = null;
		try {

			session = psContentDAO.getSessionFactory().openSession();
			Transaction tx = session.beginTransaction();

			for (TbPsContent pc : contentList) {

				TbLogPsContent lpc = new TbLogPsContent();
				TbLogPlanStep lps = new TbLogPlanStep();

				lps.setPsId(logPlanStepId);
				System.out
						.println(lps.getPsId()
								+ "-/-/-/-*/-*/-*/-*/-*/-*/logplanstepID*-/-*/*-/-*/-/-/--*/-*/-*/-/-/-*/-*");
				// 保存对应的步骤ID
				lpc.setTbLogPlanStep(lps);

				lpc.setPscContent(pc.getPscContent());

				System.out
						.println(lpc.getPscContent()
								+ "-/-*/*/-/*-/-/*-/-*/-*/-*/Content*-/-*/-*/-*/*-/-*/-*/-*/-*/");

				session.save(lpc);
			}

			tx.commit();

		} catch (AgException e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public String checkEventIfOnDealing(String eventId) {
		// TODO Auto-generated method stub
		TbCurrentEvent currentEvent = currentEventDAO.findById(eventId);
		//Long number = currentEvent.getCeNumber();
		Integer number = currentEvent.getCeNumber();
		if (number != null) {
			return "exist";
		} else {
			return "null";
		}
	}

	/*
	 * 增加指挥长
	 * 
	 * @see com.ag.zrt.service.IPlanStepService#addAdmin(java.lang.String)
	 */
	@Override
	public String addAdmin(String eventId) {
		// TODO Auto-generated method stub
		try {

			return eventDAO.findById(eventId).getTbUserByTbUId().getUId();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 增加新处理事件到当前处理事件表中，并设置当前步骤序号为1,即第一步
	 */
	@Override
	public void updateCurrentEvent(String eventId) {
		// TODO Auto-generated method stub

		TbCurrentEvent curEvent = new TbCurrentEvent();

		curEvent.setEId(eventId);

		//curEvent.setCeNumber(1L);
		curEvent.setCeNumber(1);

		String adminId = addAdmin(eventId);

		System.out
				.println(adminId
						+ "///////adminId//////////////////////////////////////////////////////////////////");

		curEvent.setEAdmin(adminId); // 增加指挥长

		currentEventDAO.attachDirty(curEvent);

	}

	/*
	 * 从预案步骤中读取所有步骤放到已启动预案步骤
	 */
	@Override
	public List<TbLogPlanStep> createLogPlanStepList(
			List<TbPlanStep> planStepList, String eventId) {
		// TODO Auto-generated method stub
		try {
			if (planStepList.size() != 0) {

				System.out.println("事件ＩＤ*********************" + eventId);

				List<TbLogPlanStep> logPlanStepList = new ArrayList<TbLogPlanStep>();

				TbPlanStep planStep = planStepList.get(0); // 取得第一个预案步骤
				TbEvent event = new TbEvent();
				TbPlan plan = new TbPlan();
				TbLogPlanStep logPlanStepForId = new TbLogPlanStep();// 保存下一环节的ID

				event.setEId(eventId);// 设置事件ID
				plan.setPId(planStep.getTbPlan().getPId());// 设置预案ID

				String frontId = IdService.findTablePK(IDUtil.TB_LOG_PLAN_STEP);// 上一步骤ＩＤ
				String nextId = IdService.findTablePK(IDUtil.TB_LOG_PLAN_STEP);// 下一步骤ＩＤ
				// 复制值
				TbLogPlanStep logPlanStepHead = fillValues(planStep, event,
						plan, frontId);

				logPlanStepDAO.save(logPlanStepHead);

				logPlanStepForId.setPsId(nextId);
				// 设置上一步骤的下一步骤入口
				logPlanStepHead = setNextPlanId(logPlanStepHead,
						logPlanStepForId, planStep);

				logPlanStepHead.setA("1");

				// logPlanStepDAO.save(logPlanStepHead);
				// 复制步骤对应内容
				createLogPlanStepContent(planStep.getPsId(), logPlanStepHead
						.getPsId());

				// // 当没有下一步时
				// if (logPlanStepHead.getTbLogPlanStep() == null) {
				// logPlanStepDAO.save(logPlanStepHead);
				// }

				logPlanStepList.add(logPlanStepHead);

				TbLogPlanStep logPlanStepFront = logPlanStepHead;// 上一步

				while (planStep.getTbPlanStep() != null) {
					System.out
							.println("进入——while (planStep.getTbPlanStep() != null) {——****************************************************");
					planStep = planStepDAO.findById(planStep.getTbPlanStep()
							.getPsId());

					frontId = nextId;
					// 下一步
					TbLogPlanStep logPlanStepNext = fillValues(planStep, event,
							plan, frontId);

					saveByOrder(logPlanStepFront, logPlanStepNext);

					logPlanStepFront = logPlanStepNext;

					nextId = IdService.findTablePK(IDUtil.TB_LOG_PLAN_STEP);

					logPlanStepForId.setPsId(nextId);

					logPlanStepFront = setNextPlanId(logPlanStepFront,
							logPlanStepForId, planStep);

					// logPlanStepDAO.attachDirty(logPlanStepFront);

					createLogPlanStepContent(planStep.getPsId(),
							logPlanStepFront.getPsId());

					logPlanStepList.add(logPlanStepFront);

				}

				updateCurrentEvent(eventId);

				return logPlanStepList;

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public TbLogPlanStep fillValues(TbPlanStep planStep, TbEvent event,
			TbPlan plan, String frontId) {

		TbLogPlanStep logPlanStep = new TbLogPlanStep();
		try {
			logPlanStep.setPsId(frontId);
			logPlanStep.setTbEvent(event);
			logPlanStep.setTbPlan(plan);

			logPlanStep.setPsName(planStep.getPsName());
			logPlanStep.setPsContent(planStep.getPsContent());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return logPlanStep;
	}

	public TbLogPlanStep setNextPlanId(TbLogPlanStep logPlanStep,
			TbLogPlanStep logPlanStepForId, TbPlanStep planStep) {
		// 判断是否最后一步
		if (planStep.getTbPlanStep() != null) {
			logPlanStep.setTbLogPlanStep(logPlanStepForId);
		}
		// else {
		// logPlanStepDAO.save(logPlanStep);//没有下一步时，保存
		// }
		return logPlanStep;

	}

	/*
	 * 先存下一步骤（无下一步骤ＩＤ），再存上一步骤
	 */
	public void saveByOrder(TbLogPlanStep front, TbLogPlanStep next) {
		logPlanStepDAO.save(next);
		logPlanStepDAO.attachDirty(front);
	}
}
